import {IonicApp,Page,NavController,Alert,NavParams,Loading,Slides} from 'ionic-angular';
import {ViewChild} from 'angular2/core';
import {Common} from '../../providers/common';
import {ArticleViewPage} from '../article-view/article-view'

@Page({
  templateUrl: 'build/pages/article-list/article-list.html',
  queries: {
    slider: new ViewChild('mySlider')
  }
})
export class ArticleListPage {
  static get parameters() {
    return [[IonicApp], [NavController], [Common],[NavParams]];
  }
  constructor(app,nav,common,navParams) {
    this.slideOptions = {
			loop: true
		};
    
    this.app = app;
    this.nav = nav;
    this.common = common;
    
    let loading = Loading.create({
      content: 'Loading...'
    });

    this.nav.present(loading); 
    //这里有个ionic2的BUG，在切换导航是会报错，暂时这么处理
    setTimeout(()=>{
      this.common.getTodayArticles().then(data=>{
        this.top_stories = data.top_stories;
        this.stories = data.stories;
        loading.dismiss();
      });
    },500);
    
     
  }
  itemSelected(article){
    let loading = Loading.create({
      content: 'Loading...'
    });
    this.nav.present(loading); 
    this.common.getArticleContentByID(article.id).then(data=>{
      loading.dismiss();
      this.nav.push(ArticleViewPage, {
       content: data
      });
    });
  }
  onSlideChanged(){
    // let currentIndex = this.slider.getActiveIndex();
    // let top_storie = this.top_stories.find((a,b)=>{
    //   if(currentIndex == b){
    //     return true;
    //   }
    // });
    
    // this.nav.present(loading); 
    // this.common.getArticleContentByID(top_storie.id).then(data=>{
    //   loading.dismiss();
    //   this.nav.push(ArticleViewPage, {
    //    content: data
    //   });
    // });
    
    // let alert = Alert.create({
    //   subTitle: top_storie.title,
    //   buttons: ['OK']
    // });
    // this.nav.present(alert);
  }
  slideTaped(item) {
    let loading = Loading.create({
      content: 'Loading...'
    });
    this.nav.present(loading); 
    this.common.getArticleContentByID(item.id).then(data=>{
      loading.dismiss();
      this.nav.push(ArticleViewPage, {
       content: data
      });
    });
  }
}
